package com.fosdata.dpm.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author yhsh
 * @version 1.0
 * @since JDK 1.8.0_21
 * 类说明：  日期类
 */
public class DateUtil {

    public static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

    public static final String DATE_FORMAT_All = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_DAY = "yyyy-MM-dd";
    public static final String DATE_FORMAT_MONTH_DAY = "MM-dd";
    public static final String DATE_FORMAT_HOUR = "HH:mm:ss";

    public static final String DATE_FORMAT_HOUR_Minute = "HHmm";
    public static final String FULL_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_DAY_HOURSE = "yyyy-MM-dd HH";
    public static final String DATE_FORMAT_MONTH = "yyyy-MM";
    public static final String FULL_DATE_FORMAT_SQL = "yyyyMMddHHmm";
    public static final String FULL_SECOND_FORMAT_SQL = "yyyyMMddHHmmss";
    public static final String DATE_FORMAT_DAY_SQL = "yyyyMMdd";
    public static final String DATE_FORMAT_All_MINUTE = "yyyy-MM-dd HH:mm";

    public static final String DATE_FORMAT_CHINESE = "yyyy年MM月dd日";


    public static String transfer2SqlFormat(String d) {
        return DateUtil.fmtDateToStr(DateUtil.formatDate2yyyyMMdd(d), DATE_FORMAT_DAY_SQL);
    }

    /**
     * 日期转化为cron表达式
     *
     * @param date
     * @return
     */
    public static String getCron(Date date) {
        String dateFormat = "ss mm HH dd MM ? yyyy";
        return DateUtil.fmtDateToStr(date, dateFormat);
    }


    /**
     * cron表达式转为日期
     *
     * @param cron
     * @return
     */
    public static Date getCronToDate(String cron) {
        String dateFormat = "ss mm HH dd MM ? yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = null;
        try {
            date = sdf.parse(cron);
        } catch (ParseException e) {
            return null;
        }
        return date;
    }

    /**
     * 获取昨天日期
     */
    public static String getYesterdayYYYYMMDD() {
        Date yesterday = DateUtil.addDays(new Date(), -1);
        return DateUtil.fmtDateToStr(yesterday, DateUtil.DATE_FORMAT_DAY);
    }

    /**
     * 获取昨天日期
     */
    public static String getTodayYYYYMMDDHHMMSS() {
        return DateUtil.fmtDateToStr(new Date(), DateUtil.DATE_FORMAT_All);
    }


    /**
     * Description:格式化日期,String字符串转化为Date
     *
     * @param date
     * @param dtFormat 例如:yyyy-MM-dd HH:mm:ss yyyyMMdd
     * @return
     */
    public static String fmtDateToStr(Date date, String dtFormat) {
        if (date == null) {
            return "";
        }
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(dtFormat);
            return dateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 默认的日期格式
     */
    public static String fmtDateToDefaultStr(Date date) {
        if (date == null) {
            return "";
        }
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_All);
            return dateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static Date strParseDate(String time, String dtFormat) {
        if (StringUtils.isEmpty(time)) {
            return null;
        }
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(dtFormat);
            return dateFormat.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * localDate转日期
     **/
    public static String localDateToDateStr(LocalDate date) {
        if (null == date) {
            return "";
        }
        String format = date.format(DateTimeFormatter.ofPattern(DATE_FORMAT_DAY));
        return format;

    }


    /**
     * localDate转日期
     **/
    public static String localDateTimeToStr(LocalDateTime date, String formatStr) {
        if (null == date) {
            return "";
        }
        String format = date.format(DateTimeFormatter.ofPattern(formatStr));
        return format;

    }

    /**
     * localDate转日期
     **/
    public static LocalDateTime strToLocalDateTime(String date, String formatStr) {
        if (null == date) {
            return null;
        }
        return LocalDateTime.parse(date, DateTimeFormatter.ofPattern(formatStr));
    }


    /**
     * 获取oracle时间类型
     * 返回值类型为Timestamp
     */
    public static Timestamp currentDate() {
        return new Timestamp(System.currentTimeMillis());
    }


    public static String LocalDateTime() {
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    public static LocalDate strToLocalDate(String date) {
        LocalDate localDate = LocalDate.parse(date);
        return localDate;
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getNowTime() {
        return DateUtil.getCurrentDate(DATE_FORMAT_All);
    }

    public static Date getLastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);//昨天
        return calendar.getTime();
    }

    /**
     * 获取当前日期
     *
     * @param format
     * @return
     */
    public static String getCurrentDate(String format) {
        return new SimpleDateFormat(format).format(new Date());
    }

    /**
     * 格式化时间 HH:mm
     *
     * @param dateVale
     **/
    public static Date formatDateHHmm(String dateVale) {
        SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_HOUR);
        Date date = null;
        try {
            date = dateformat.parse(dateVale);
        } catch (ParseException e) {
            logger.error("error: string date convert date error");
        }
        return date;
    }

    /**
     * 格式化时间 yyyy-MM
     *
     * @param dateVale
     **/
    public static Date formatDateYYYYMM(String dateVale) {
        SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_MONTH);
        Date date = null;
        try {
            date = dateformat.parse(dateVale);
        } catch (ParseException e) {
            logger.error("error: string date convert date error");
        }
        return date;
    }


    /**
     * 格式化时间 yyyy-MM-dd HH:mm:ss
     *
     * @param dateVale
     **/
    public static Date formatDate2yyyyMMddHHmmss(String dateVale) {

        SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_All);
        Date date = null;
        try {
            date = dateformat.parse(dateVale);
        } catch (ParseException e) {
            logger.error("error: string date convert date error");
        }
        return date;
    }

    /**
     * 格式化时间 yyyy-MM-dd
     *
     * @param dateVale
     **/
    public static Date formatDate2yyyyMMdd(String dateVale) {

        SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_DAY);
        Date date = null;
        try {
            date = dateformat.parse(dateVale);
        } catch (ParseException e) {
            logger.error("error: string date convert date error");
        }
        return date;
    }

    /**
     * 格式化时间 yyyy-MM
     *
     * @param dateVale
     **/
    public static Date formatDate2yyyyMM(String dateVale) {

        SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_MONTH);
        Date date = null;
        try {
            date = dateformat.parse(dateVale);
        } catch (ParseException e) {
            logger.error("error: string date convert date error");
        }
        return date;
    }

    /**
     * 格式化时间 yyyy-MM-dd
     *
     * @param dateVale
     **/
    public static Date formatDate2yyyyMMddHH(String dateVale) {

        SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_DAY_HOURSE);
        Date date = null;
        try {
            date = dateformat.parse(dateVale);
        } catch (ParseException e) {
            logger.error("error: string date convert date error");
        }
        return date;
    }


    /**
     * 把日期字符串格式化成日期类型
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static Date convert2Date(String dateStr, String format) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        }
        if (format.equals(FULL_DATE_FORMAT_SQL)) {
            if (dateStr.length() > 12) {
                format = FULL_SECOND_FORMAT_SQL;
            }
        }
        SimpleDateFormat simple = new SimpleDateFormat(format);
        try {
            simple.setLenient(false);
            return simple.parse(dateStr);
        } catch (Exception e) {
            logger.error(dateStr + "error:string date convert to date", e);
            return null;
        }
    }


    /**
     * 把日期类型格式化成字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String convert2String(Date date, String format) {
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            return formater.format(date);
        } catch (Exception e) {
            logger.error(date + "error: date convert to string date");
            return null;
        }
    }

    /**
     * 转sql的time格式
     *
     * @param date
     * @return
     */
    public static Timestamp convertSqlTime(Date date) {
        Timestamp timestamp = new Timestamp(date.getTime());
        return timestamp;
    }

    /**
     * 转sql的日期格式
     *
     * @param date
     * @return
     */
    public static java.sql.Date convertSqlDate(Date date) {
        java.sql.Date Datetamp = new java.sql.Date(date.getTime());
        return Datetamp;
    }


    /**
     * 获取时间戳
     *
     * @return
     */
    public static long getTimestamp() {
        return System.currentTimeMillis();
    }

    /**
     * 获取月份的天数
     *
     * @param year
     * @param month
     * @return
     */
    public static int getDaysOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取日期的年
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取日期的年
     *
     * @param date
     * @return
     */
    public static String getYear(String date) {
        Date d = formatDateYYYYMM(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        return calendar.get(Calendar.YEAR) +"";
    }

    /**
     * 获取一个月中指定第几周的开始日期和结束日期
     */
    public static Pair<String, String> getStartDayAndEndDayOfWeek(String month, int whichWeek) {
        Date d = formatDateYYYYMM(month);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        int fistDayOfMonthInWeek = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.add(Calendar.DAY_OF_YEAR, -(fistDayOfMonthInWeek - 2));
        calendar.add(Calendar.DAY_OF_YEAR, (whichWeek - 1) * 7);
        Date start = calendar.getTime();
        calendar.add(Calendar.DAY_OF_YEAR, 6);
        Date end = calendar.getTime();
        if (end.getTime() > lastDayOfMonth(month).getTime()) {
            return null;
        }
        return Pair.of(fmtDateToStr(start, DATE_FORMAT_DAY), fmtDateToStr(end, DATE_FORMAT_DAY));
    }


    public static Date lastDayOfMonth(String month) {
        return lastDayOfMonth(formatDateYYYYMM(month));
    }


    /**
     * 获取一个月的最后一天
     */
    public static Date lastDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.roll(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }


    public static int getDaysOfMonth(String month) {
        return getDaysOfMonth(formatDateYYYYMM(month));
    }


    /**
     * 获取一个月有多少天
     */
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }


    /**
     * 获取月份值
     */
    public static int getMonth(String month) {
        Date d = formatDateYYYYMM(month);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        return calendar.get(Calendar.MONTH)+1;
    }

    /**
     * 获取日期的月
     *
     * @param date
     * @return
     */
    public static String getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        return month < 10 ? "0" + month : month + "";
    }

    public static boolean isFebruary(String endDate) {
        String month = DateUtil.getMonth(DateUtil.formatDate2yyyyMMdd(endDate));
        return month.equals("02");
    }

    /**
     * 获取上个月
     **/
    public static String getLastMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        int year3 = calendar.get(Calendar.YEAR);
        int month3 = calendar.get(Calendar.MONTH) + 1;
        return year3 + "-" + month3;
    }


    /**
     * 获取当月及前num个月的月份
     **/
    public static List<String> getLastMonths(int num) {
        List<String> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        list.add(monthFormat(calendar));
        for (int i = 0; i < num; i++) {
            calendar.add(Calendar.MONTH, -1);
            list.add(monthFormat(calendar));
        }
        Collections.sort(list);
        return list;
    }


    private static String monthFormat(Calendar calendar) {
        String month = null;
        int year3 = calendar.get(Calendar.YEAR);
        int month3 = calendar.get(Calendar.MONTH) + 1;
        if (month3 < 10) {
            month = year3 + "-0" + month3;
        } else {
            month = year3 + "-" + month3;
        }
        return month;
    }


    /**
     * 获取日期的日
     *
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DATE);
    }

    /**
     * 获取日期的日
     *
     * @param date
     * @return
     */
    public static String get30Day(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Integer day = calendar.get(Calendar.DATE);
        return day < 10 ? "0" + day : day + "";
    }

    /**
     * 获取日期的时
     *
     * @param date
     * @return
     */
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR);
    }

    /**
     * 获取日期的时24小时
     *
     * @param date
     * @return
     */
    public static int get24Hour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取日期的分种
     *
     * @param date
     * @return
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 获取日期的分种
     *
     * @param date
     * @return
     */
    public static String get30Minute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int num = calendar.get(Calendar.MINUTE);
        return num > 0 ? num + "" : "0" + num;
    }

    /**
     * 获取日期的秒
     *
     * @param date
     * @return
     */
    public static int getSecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }

    /**
     * 获取星期几
     *
     * @param date
     * @return
     */
    public static int getWeekDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        return dayOfWeek - 1;
    }

    /**
     * 获取星期几
     *
     * @param date
     * @return
     */
    public static String getWeekDayStr(String date) throws ParseException {
        String week = "";
        SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT_DAY);
        Date parse = dateformat.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parse);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        int i = dayOfWeek - 1;
        switch (i) {
            case 0:
                week = "星期天";
                break;
            case 1:
                week = "星期一";
            case 2:
                week = "星期二";
                break;
            case 3:
                week = "星期三";
                break;
            case 4:
                week = "星期四";
                break;
            case 5:
                week = "星期五";
                break;
            case 6:
                week = "星期六";
                break;
            default:
                week = "";
        }
        return week;
    }

    /**
     * 获取哪一年共有多少周
     *
     * @param year
     * @return
     */
    public static int getMaxWeekNumOfYear(int year) {
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        return getWeekNumOfYear(c.getTime());
    }

    /**
     * 取得某天是一年中的多少周
     *
     * @param date
     * @return
     */
    public static int getWeekNumOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);
        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 取得某天所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
        return c.getTime();
    }

    /**
     * 获取当前时间是本月第几周
     **/
    public static int getWeekOfMonth(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
        return weekOfMonth;
    }

    /**
     * 取得某天所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
        return c.getTime();
    }

    /**
     * 取得某年某周的第一天 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-05为2009年第一周的第一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {
        Calendar calFirst = Calendar.getInstance();
        calFirst.set(year, 0, 7);
        Date firstDate = getFirstDayOfWeek(calFirst.getTime());

        Calendar firstDateCal = Calendar.getInstance();
        firstDateCal.setTime(firstDate);

        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, (week - 1) * 7);
        firstDate = getFirstDayOfWeek(cal.getTime());

        return firstDate;
    }

    /**
     * 取得某年某周的最后一天 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周, 2009-01-04为
     * 2008年最后一周的最后一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        Calendar calLast = Calendar.getInstance();
        calLast.set(year, 0, 7);
        Date firstDate = getLastDayOfWeek(calLast.getTime());

        Calendar firstDateCal = Calendar.getInstance();
        firstDateCal.setTime(firstDate);

        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, (week - 1) * 7);
        Date lastDate = getLastDayOfWeek(cal.getTime());

        return lastDate;
    }


    private static Date add(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        } else {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(calendarField, amount);
            return c.getTime();
        }
    }

    /*
     * 1则代表的是对年份操作， 2是对月份操作， 3是对星期操作， 5是对日期操作， 11是对小时操作， 12是对分钟操作， 13是对秒操作，
     * 14是对毫秒操作
     */

    /**
     * 增加年
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addYears(Date date, int amount) {
        return add(date, 1, amount);
    }

    /**
     * 增加月
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addMonths(Date date, int amount) {
        return add(date, 2, amount);
    }

    /**
     * 增加周
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addWeeks(Date date, int amount) {
        return add(date, 3, amount);
    }


    /**
     * 增加天
     *
     * @param date
     * @param amount
     * @return
     */
    public static String addDays(String date, int amount) {
        Date afterAdd = add(DateUtil.formatDate2yyyyMMdd(date), 5, amount);
        return DateUtil.fmtDateToStr(afterAdd, DATE_FORMAT_DAY);
    }

    /**
     * 增加天
     *
     * @param date
     * @param amount
     * @return
     */
    public static String addDays(String date, int amount, String format) {
        Date afterAdd = add(DateUtil.strParseDate(date, format), 5, amount);
        return DateUtil.fmtDateToStr(afterAdd, format);
    }


    /**
     * 增加天
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addDays(Date date, int amount) {
        return add(date, 5, amount);
    }

    /**
     * 增加时
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addHours(Date date, int amount) {
        return add(date, 11, amount);
    }

    /**
     * 增加分
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addMinutes(Date date, int amount) {
        return add(date, 12, amount);
    }

    /**
     * 增加秒
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addSeconds(Date date, int amount) {
        return add(date, 13, amount);
    }

    /**
     * 增加毫秒
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addMilliseconds(Date date, int amount) {
        return add(date, 14, amount);
    }


    /**
     * time差
     *
     * @param before
     * @param after
     * @return
     */
    public static long diffTimes(Date before, Date after) {
        return after.getTime() - before.getTime();
    }


    public static Long diffSecond(String before, String after) {
        if (StringUtils.isEmpty(before) || StringUtils.isEmpty(after)) {
            return 0L;
        }
        return (DateUtil.formatDate2yyyyMMddHHmmss(after).getTime()
                - DateUtil.formatDate2yyyyMMddHHmmss(before).getTime()) / 1000;
    }

    public static Long diffSecond(LocalDateTime before, LocalDateTime after) {
        if (null == before || null == after) {
            return 0L;
        }
        Duration duration = Duration.between(before, after);
        return duration.getSeconds();
    }

    public static String secondToDay(Long seconds) {
        return new BigDecimal(seconds).divide(new BigDecimal(60 * 60 * 24), 2, BigDecimal.ROUND_HALF_UP).toString();
    }


    /**
     * 秒差
     *
     * @param before
     * @param after
     * @return
     */
    public static long diffSecond(Date before, Date after) {
        return (after.getTime() - before.getTime()) / 1000;
    }

    /**
     * 分种差
     *
     * @param before
     * @param after
     * @return
     */
    public static int diffMinute(Date before, Date after) {
        return (int) (after.getTime() - before.getTime()) / 1000 / 60;
    }


    /**
     * 分种差
     *
     * @param before
     * @param after
     * @return
     */
    public static Long diffMinuteLong(Date before, Date after) {
        if (null == before || null == after) {
            return null;
        }
        Double l = (after.getTime() - before.getTime()) / 1000 / 60.0;
        return l.longValue();
    }


    /**
     * 毫秒差
     *
     * @param before
     * @param after
     * @return
     */
    public static Long diffMillsecondLong(Date before, Date after) {
        return after.getTime() - before.getTime();
    }

    /**
     * 毫秒差
     *
     * @param before
     * @param after
     * @return
     */
    public static Long diffMillsecondLong(String before, String after) {
        if (StringUtils.isEmpty(before) || StringUtils.isEmpty(after)) {
            return null;
        }
        return diffMillsecondLong(formatDate2yyyyMMddHHmmss(before), formatDate2yyyyMMddHHmmss(after));
    }


    /**
     * 分种差
     *
     * @param aDay
     * @param bDay
     * @return
     */
    public static Long diffMinuteLong(String aDay, String bDay) {
        if (StringUtils.isEmpty(aDay) || StringUtils.isEmpty(bDay)) {
            return null;
        }
        return diffMinuteLong(formatDate2yyyyMMddHHmmss(bDay), formatDate2yyyyMMddHHmmss(aDay));
    }


    public static long diffHour(String bDay, String aDay) {
        return diffHour(formatDate2yyyyMMddHHmmss(bDay), formatDate2yyyyMMddHHmmss(aDay));
    }

    /**
     * 分种差
     *
     * @param aDay
     * @param bDay
     * @return
     */
    public static Long diffMinuteLong(Date aDay, String bDay) {
        if (null == aDay || StringUtils.isEmpty(bDay)) {
            return null;
        }
        return diffMinuteLong(formatDate2yyyyMMddHHmmss(bDay), aDay);
    }


    /**
     * 时差
     *
     * @param before
     * @param after
     * @return
     */
    public static long diffHour(Date before, Date after) {
        return new BigDecimal(after.getTime() - before.getTime()).divide(new BigDecimal(1000 * 60 * 60), 1, BigDecimal.ROUND_HALF_UP).longValue();
    }

    /**
     * 天数差
     *
     * @param before
     * @param after
     * @return
     */
    public static int diffDay(Date before, Date after) {
        return Integer.parseInt(String.valueOf(((after.getTime() - before.getTime()) / 86400000)));
    }

    public static int diffDayYYYYMMdd(String aDay, String bDay) {
        return diffDay(formatDate2yyyyMMdd(bDay), formatDate2yyyyMMdd(aDay));
    }

    /**
     * 获取天数差: aDay - bDay
     */
    public static int diffDay(String aDay, String bDay) {
        return diffDay(formatDate2yyyyMMddHHmmss(bDay), formatDate2yyyyMMddHHmmss(aDay));
    }


    /**
     * 月差
     *
     * @param before
     * @param after
     * @return
     */
    public static int diffMonth(Date before, Date after) {
        int monthAll = 0;
        int yearsX = diffYear(before, after);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(before);
        c2.setTime(after);
        int monthsX = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        monthAll = yearsX * 12 + monthsX;
        int daysX = c2.get(Calendar.DATE) - c1.get(Calendar.DATE);
        if (daysX > 0) {
            monthAll = monthAll + 1;
        }
        return monthAll;
    }

    /**
     * 年差
     *
     * @param before
     * @param after
     * @return
     */
    public static int diffYear(Date before, Date after) {
        return getYear(after) - getYear(before);
    }

    /**
     * 设置23:59:59
     *
     * @param date
     * @return
     */
    public static Date setEndDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 设置00:00:00
     *
     * @param date
     * @return
     */
    public static Date setStartDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 00);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);
        return calendar.getTime();
    }


    public static String getFirstDayStrOfLastYear() {
        return fmtDateToStr(getFirstDayOfLastYear(), DATE_FORMAT_DAY);
    }


    public static String getFirstDayStrOfLastYearByMonth(String month) {
        return fmtDateToStr(getFirstDayOfLastYearByMonth(month), DATE_FORMAT_DAY);
    }


    public static String getLastDayStrOfLastYear() {
        return fmtDateToStr(getLastDayOfLastYear(), DATE_FORMAT_DAY);
    }


    public static String getLastDayStrOfLastYearByMonth(String month) {
        return fmtDateToStr(getLastDayOfLastYearByMonth(month), DATE_FORMAT_DAY);
    }


    /**
     * 获取去年第一天
     */
    public static Date getFirstDayOfLastYear() {
        Calendar currCal = Calendar.getInstance();
        currCal.add(Calendar.YEAR, -1);
        int lastYear = currCal.get(Calendar.YEAR);
        return getYearFirst(lastYear);
    }

    public static Date getFirstDayOfLastYearByMonth(String month) {
        Date currentMonth = DateUtil.formatDateYYYYMM(month);
        Calendar currCal = Calendar.getInstance();
        currCal.setTime(currentMonth);
        currCal.add(Calendar.YEAR, -1);
        int lastYear = currCal.get(Calendar.YEAR);
        return getYearFirst(lastYear);
    }


    /**
     * 获取去年最后一天
     */
    public static Date getLastDayOfLastYear() {
        Calendar currCal = Calendar.getInstance();
        currCal.add(Calendar.YEAR, -1);
        int lastYear = currCal.get(Calendar.YEAR);
        return getYearLast(lastYear);
    }


    public static Date getLastDayOfLastYearByMonth(String month) {
        Date currentMonth = DateUtil.formatDateYYYYMM(month);
        Calendar currCal = Calendar.getInstance();
        currCal.setTime(currentMonth);
        currCal.add(Calendar.YEAR, -1);
        int lastYear = currCal.get(Calendar.YEAR);
        return getYearLast(lastYear);
    }

    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        return currYearLast;
    }


    public static String getFirstDayStrOfMonth(Date date) {
        Date firstDayOfMonth = getFirstDayOfMonth(date);
        return fmtDateToStr(firstDayOfMonth, DATE_FORMAT_DAY);
    }

    /**
     * 获取上个月的第一天
     */
    public static String getFirstDayStrOfLastMonth() {
        Date lastMonth = addMonths(new Date(), -1);
        Date firstDayOfMonth = getFirstDayOfMonth(lastMonth);
        return fmtDateToStr(firstDayOfMonth, DATE_FORMAT_DAY);
    }

    /**
     * 获取上个月的第一天
     */
    public static String getFirstDayStrOfLastMonth(Date date) {
        Date lastMonth = addMonths(date, -1);
        Date firstDayOfMonth = getFirstDayOfMonth(lastMonth);
        return fmtDateToStr(firstDayOfMonth, DATE_FORMAT_DAY);
    }


    /**
     * 获取上个月的第一天
     */
    public static String getLastDayStrOfLastMonth() {
        Date lastMonth = addMonths(new Date(), -1);
        Date firstDayOfMonth = getFirstDayOfMonth(lastMonth);
        return fmtDateToStr(firstDayOfMonth, DATE_FORMAT_DAY);
    }


    /**
     * 获取上个月的最后一天
     */
    public static String getLastDayStrOfLastMonth(Date date) {
        Date lastMonth = addMonths(date, -1);
        Date lastDayOfMonth = getLastDayOfMonth(lastMonth);
        return fmtDateToStr(lastDayOfMonth, DATE_FORMAT_DAY);
    }


    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        return DateUtil.getFirstDayOfMonth(year, month + 1);
    }


    public static String getLastDayStrOfMonth(Date date) {
        Date firstDayOfMonth = getLastDayOfMonth(date);
        return fmtDateToStr(firstDayOfMonth, DATE_FORMAT_DAY);
    }


    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        return DateUtil.getLastDayOfMonth(year, month + 1);
    }

    /**
     * 获取指定年月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getFirstDayOfMonth(Integer year, Integer month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        return cal.getTime();
    }

    /**
     * 获取指定年月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        return cal.getTime();
    }

    /**
     * 根据季度 ，年份 获取此季度的第一天
     */
    public static Date getFirstQuarterOfDay(Integer year, Integer quarter) {
        switch (quarter) {
            case 1:
                return getFirstDayOfMonth(year, 1);
            case 2:
                return getFirstDayOfMonth(year, 4);
            case 3:
                return getFirstDayOfMonth(year, 7);
            default:
                return getFirstDayOfMonth(year, 10);
        }
    }


    /**
     * 根据季度，年份 获取此季度的最后天
     */
    public static Date getLastQuarterOfDay(Integer year, Integer quarter) {
        switch (quarter) {
            case 1:
                return getLastDayOfMonth1(year, 1);
            case 2:
                return getLastDayOfMonth1(year, 4);
            case 3:
                return getLastDayOfMonth1(year, 7);
            default:
                return getLastDayOfMonth1(year, 10);
        }
    }

    /**
     * 获取指定年月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth1(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);

        cal.add(Calendar.MONTH, 2); // 加一个月
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        return cal.getTime();
    }


    /**
     * TimeConvert 工具类
     *
     * @param date
     * @param dateFormatType
     * @return
     */
    public static String getDateFormat(Date date, String dateFormatType) {
        SimpleDateFormat simformat = new SimpleDateFormat(dateFormatType);
        return simformat.format(date);
    }

    public static String formatCSTTime(String date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
            Date d = sdf.parse(date);
            return DateUtil.getDateFormat(d, format);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 0相等    1大于      -1 小于
     * 比较年月日
     *
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static int compareToDay(String date1, String date2) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = format.parse(date1);
        Date d2 = format.parse(date2);

        int row = d1.compareTo(d2);
        return row;
    }

    /**
     * 比较时间  0相等    1大于      -1 小于
     *
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static int compareToTime(String date1, String date2) throws ParseException {
        DateFormat format = new SimpleDateFormat("HH:mm");
        Date d1 = format.parse(date1);
        Date d2 = format.parse(date2);

        int row = d1.compareTo(d2);
        return row;
    }

    /**
     * 比较时间  0相等    1大于      -1 小于
     *
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static int compareToDayTime(String date1, String date2) {
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date d1 = format.parse(date1);
            Date d2 = format.parse(date2);
            int row = d1.compareTo(d2);
            return row;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 比较时间  0相等    1大于      -1 小于
     *
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static int compareToDayTimeDay(String date1, String date2) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = format.parse(date1);
        Date d2 = format.parse(date2);

        int row = d1.compareTo(d2);
        return row;
    }

    /**
     * 获取年月日
     *
     * @return
     */
    public static String YTD() {
        String string = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        return string;
    }

    /**
     * 根据时间获取时间 年月日时分
     *
     * @param date1
     * @return
     * @throws ParseException
     */
    public static String YTDTimes(String date1) throws ParseException {
        DateFormat format = new SimpleDateFormat(DateUtil.FULL_DATE_FORMAT);
        //开课时间
        Date startCourse = format.parse(date1);
        String string = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(startCourse);
        return string;
    }

    /**
     * 获取时间 时分
     *
     * @return
     */
    public static String Time() {
        String string = new SimpleDateFormat("HH:mm").format(new Date());
        return string;
    }

    /**
     * 根据时间获取时间年月日
     *
     * @param date1
     * @return
     * @throws ParseException
     */
    public static String YTD(String date1) throws ParseException {
        DateFormat format = new SimpleDateFormat(DateUtil.FULL_DATE_FORMAT);
        //开课时间
        Date startCourse = format.parse(date1);
        String string = new SimpleDateFormat("yyyy-MM-dd").format(startCourse);
        return string;
    }


    /**
     * 根据时间获取时间 月日
     *
     * @return
     */
    public static String TD(String date1) throws ParseException {
        DateFormat format = new SimpleDateFormat(DateUtil.FULL_DATE_FORMAT);
        Date startCourse = format.parse(date1);
        String string = new SimpleDateFormat("MM-dd").format(startCourse);
        return string;
    }

    /**
     * 根据时间获取时间 时分
     *
     * @param date1
     * @return
     * @throws ParseException
     */
    public static String Times(String date1) throws ParseException {
        DateFormat format = new SimpleDateFormat(DateUtil.FULL_DATE_FORMAT);
        //开课时间
        Date startCourse = format.parse(date1);
        String string = new SimpleDateFormat("HH:mm").format(startCourse);
        return string;
    }


    /**
     * 2020-08-04T15:38:32.7161581+08:00 转  str
     *
     * @param createTime
     * @return
     * @throws ParseException
     */
    public static String strTimestampStr(Object createTime) {

        try {
            String date = (String) createTime;
            if (StringUtils.isNotBlank(date)) {
                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                Date date1 = format1.parse(date);
                return new Timestamp(date1.getTime()).toString();
            } else {

                return new Timestamp(System.currentTimeMillis()).toString();
            }
        } catch (ParseException e) {
            return new Timestamp(System.currentTimeMillis()).toString();
        }

    }


    public static String strTimestampStr1(Object createTime) throws ParseException {

        String date = (String) createTime;
        if (StringUtils.isNotBlank(date)) {
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = format1.parse(date);
            return new Timestamp(date1.getTime()).toString();
        } else {

            return new Timestamp(System.currentTimeMillis()).toString();
        }

    }

    /**
     * 比较时间 课程调整业务  专用
     *
     * @param date1
     * @return
     * @throws ParseException
     */
    public static int compareCourseToTime(Integer id, String date1) throws ParseException {

        //定义一个状态值    2 无效      3 预约通知    5上课通知
        int row = 0;
        DateFormat format = new SimpleDateFormat(DateUtil.FULL_DATE_FORMAT);
        //开课时间
        Date startCourse = format.parse(date1);
        //当前时间
        Date time = format.parse(DateUtil.getCurrentDate(DateUtil.FULL_DATE_FORMAT));
        row = startCourse.compareTo(time);
        //时间差取绝对值
        Long i = DateUtil.diffMinuteLong(time, startCourse);

        if (row == 0) {
            //时间相等 上课通知
            return 5;
        } else if (row == -1) {
            if (Math.abs(i) <= 100) {
                logger.error("5直播课章节" + id + "开课时间：" + date1 + "与当前时间:" + DateUtil.getCurrentDate(DateUtil.FULL_DATE_FORMAT) + "的时间差为" + i + "分钟");
            }  // 开课时间小于当前时间 已开课
            if (Math.abs(i) <= 25) {
                return 5;
            }
        } else {
            if (Math.abs(i) <= 100) {
                logger.error("3直播课章节" + id + "开课时间：" + date1 + "与当前时间:" + DateUtil.getCurrentDate(DateUtil.FULL_DATE_FORMAT) + "的时间差为" + i + "分钟");
            }
            // 开课时间大于当前时间 还未开课
            if (Math.abs(i) <= 25) {
                return 3;
            }
            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            // 30分钟前
            calendar.add(Calendar.MINUTE, 30);
            String minute = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
            //当前时间提前前35分钟
            Date d2 = format.parse(minute);
            row = startCourse.compareTo(d2);
            Long i2 = DateUtil.diffMinuteLong(startCourse, d2);

            // 提前30分钟 相等 预约
            if (row == 0) {
                return 2;
            } else if (row == 1) {
                return 2;
            } else if (row == -1) {
                // 提前30分钟  开课时间小于提前30分钟
                if (Math.abs(i2) <= 100) {
                    logger.error("2直播课章节" + id + "开课时间：" + date1 + "与当前提前32分钟时间:" + minute + "的时间差为" + i2 + "分钟");
                }
                if (Math.abs(i2) > 33) {
                    return 5;
                }
                return 2;
            }
        }

        return 2;
    }


    /**
     * 获取起止日期
     *
     * @param sdf    需要显示的日期格式
     * @param date   需要参照的日期
     * @param n      最近n周
     * @param option 0 开始日期；1 结束日期
     * @param k      0 包含本周 1 不包含本周
     * @return
     */
    public static String getFromToDate(SimpleDateFormat sdf, Date date, int n, int option, int k) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int offset = 0 == option ? 1 - dayOfWeek : 7 - dayOfWeek;
        int amount = 0 == option ? offset - (n - 1 + k) * 7 : offset - k * 7;
        calendar.add(Calendar.DATE, amount);
        return sdf.format(calendar.getTime());
    }

    /**
     * 根据当前日期获得最近n周的日期区间（包含本周）
     *
     * @param n
     * @param sdf
     * @return
     */
    public static String getNWeekTimeInterval(int n, SimpleDateFormat sdf) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String beginDate = getFromToDate(sdf, new Date(), n, 0, 0);
        Date date = simpleDateFormat.parse(beginDate);
        Date date1 = addDays(date, 1);
        String date1String = formatter.format(date1);
        Date date2 = addDays(date, 2);
        String date2String = formatter.format(date2);
        Date date3 = addDays(date, 3);
        String date3String = formatter.format(date3);
        Date date4 = addDays(date, 4);
        String date4String = formatter.format(date4);
        Date date5 = addDays(date, 5);
        String date5String = formatter.format(date5);
        Date date6 = addDays(date, 6);
        String date6String = formatter.format(date6);


        return beginDate + "," + date1String + "," + date2String + "," + date3String + "," + date4String + "," + date5String + "," + date6String;
    }


    public static String getNWeekTimeIntervalNew(int n, SimpleDateFormat sdf, String time) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String beginDate = getFromToDate(sdf, formatDate2yyyyMMdd(time), n, 0, 0);
        Date date = formatter.parse(beginDate);
        Date date1 = addDays(date, 1);
        String date1String = formatter.format(date1);
        Date date2 = addDays(date, 2);
        String date2String = formatter.format(date2);
        Date date3 = addDays(date, 3);
        String date3String = formatter.format(date3);
        Date date4 = addDays(date, 4);
        String date4String = formatter.format(date4);
        Date date5 = addDays(date, 5);
        String date5String = formatter.format(date5);
        Date date6 = addDays(date, 6);
        String date6String = formatter.format(date6);


        return beginDate + "," + date1String + "," + date2String + "," + date3String + "," + date4String + "," + date5String + "," + date6String;
    }


    /**
     * 时间范围取出所有时间
     *
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static List<String> getBetweenDays(String startTime, String endTime)  {
        try {
            if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
                return new ArrayList<>();
            }
            //1、定义转换格式
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date start = df.parse(startTime);
            Date end = df.parse(endTime);
            end = addDays(end, 1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            List<String> result = new ArrayList<String>();
            Calendar tempStart = Calendar.getInstance();

            tempStart.setTime(start);
            tempStart.add(Calendar.DAY_OF_YEAR, 1);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            result.add(sdf.format(start));
            while (tempStart.before(tempEnd)) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ArrayList<>();

    }

    /**
     * 根据当前日期获得本周的日期区间（本周周一和周日日期）
     *
     * @param sdf
     * @return
     */
    public static String getThisWeekTimeInterval(SimpleDateFormat sdf) throws ParseException {
        return getNWeekTimeInterval(1, sdf);
    }

    /**
     * 根据日期获得本周的日期区间（本周周一和周日日期）
     *
     * @param sdf
     * @return
     */
    public static String getThisWeekTimeInterval(SimpleDateFormat sdf, String time) throws ParseException {
        return getNWeekTimeIntervalNew(1, sdf, time);
    }


    /**
     * 根据当前系统时间获取本月所有日期
     *
     * @param date
     * @return
     */
    public static List<Date> getAllTheDateOftheMonth(Date date) {
        List<Date> list = new ArrayList<Date>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        int month = cal.get(Calendar.MONTH);
        while (cal.get(Calendar.MONTH) == month) {
            list.add(cal.getTime());
            cal.add(Calendar.DATE, 1);
        }
        return list;
    }


    /**
     * 两个时间的集合
     *
     * @param begin
     * @param end
     * @return
     */
    public static List<String> getBetweenDates(String begin, String end) {
        try {
            List<String> result = new ArrayList<String>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar tempStart = Calendar.getInstance();
            Date beginDate = sdf.parse(begin);
            Date endDate = sdf.parse(end);
            tempStart.setTime(beginDate);
            while (beginDate.getTime() <= endDate.getTime()) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
                beginDate = tempStart.getTime();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 根据时间获取b周一 和时间
     *
     * @param times
     * @return
     */
    public static String getDateOftheMonth(String times) throws ParseException {

        //设置时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(times);
        Date dates = addDays(date, -7);
        String beginDate = sdf.format(dates);
        Date date1 = addDays(date, -6);
        String date1String = sdf.format(date1);
        Date date2 = addDays(date, -5);
        String date2String = sdf.format(date2);
        Date date3 = addDays(date, -4);
        String date3String = sdf.format(date3);
        Date date4 = addDays(date, -3);
        String date4String = sdf.format(date4);
        Date date5 = addDays(date, -2);
        String date5String = sdf.format(date5);
        Date date6 = addDays(date, -1);
        String date6String = sdf.format(date6);


        return beginDate + "," + date1String + "," + date2String + "," + date3String + "," + date4String + "," + date5String + "," + date6String;
    }


    /**
     * 根据时间获取本月所有日期
     *
     * @param time
     * @return
     */
    public static List<Date> getAllDateOftheMonth(String time) {
        List<Date> list = new ArrayList<Date>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(formatDate2yyyyMMdd(time));
        cal.set(Calendar.DATE, 1);
        int month = cal.get(Calendar.MONTH);
        while (cal.get(Calendar.MONTH) == month) {
            list.add(cal.getTime());
            cal.add(Calendar.DATE, 1);
        }
        return list;
    }


    /*  *判断是否超过多少小时 如：24
     * @param tableTime 业务时间
     * @param hour 多少小时
     * @return boolean
     * @throws Exception
     */
    public static boolean judgmentDate(String tableTime, Double hour) throws Exception {
        String currentTime = DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss");//当前时间

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
        Date start = sdf.parse(tableTime);//业务时间
        Date end = sdf.parse(currentTime);//当前时间
        long cha = end.getTime() - start.getTime();
        if (cha < 0) {
            return false;
        }
        Double result = cha * 1.0 / (1000 * 60 * 60);
        if (result.compareTo(hour) != -1) {
            return true;//是小于等于 hour 小时
        } else {
            return false;
        }
    }


    /**
     * 秒 转换 时分秒
     *
     * @param time
     * @return
     */
    public static String transfom(final int time) {
        int hh = time / 3600;
        int mm = (time % 3600) / 60;
        int ss = (time % 3600) % 60;
        return (hh < 10 ? ("0" + hh) : hh) + ":" + (mm < 10 ? ("0" + mm) : mm) + ":" + (ss < 10 ? ("0" + ss) : ss);
    }


    public static Date qitian() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date2 = new Date();
        Date date1 = addDays(date2, -7);
        String date1String = formatter.format(date1);
        return date1;
    }


    /**
     * 时分秒 转换 miao
     *
     * @param time
     * @return
     */
    public static String transfomMiao(String time) {
        long s = 0;
        boolean b = time.contains(":");
        if (b) {
            int index1 = time.indexOf(":");
            int index2 = time.indexOf(":", index1 + 1);
            s = Integer.parseInt(time.substring(0, index1)) * 3600;//小时
            s += Integer.parseInt(time.substring(index1 + 1, index2)) * 60;//分钟
            s += Integer.parseInt(time.substring(index2 + 1));//秒\
            return s + "";
        }
        return "";
    }


    /**
     * 显示上个月的第一天 和第二天
     *
     * @return
     */
    public static String GetPreMD() {

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            //获取前一个月第一天
            Calendar calendar1 = Calendar.getInstance();
            calendar1.add(Calendar.MONTH, -1);
            calendar1.set(Calendar.DAY_OF_MONTH, 1);
            String firstDay = sdf.format(calendar1.getTime());
            //获取前一个月最后一天
            Calendar calendar2 = Calendar.getInstance();
            calendar2.set(Calendar.DAY_OF_MONTH, 0);
            String lastDay = sdf.format(calendar2.getTime());
            return firstDay + "," + lastDay;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        Date today = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String result = sdf.format(today);
        return result;
    }


    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static Date getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        Date day = calendar.getTime();
        return day;
    }

    /**
     * 获取本周周天的日期
     **/
    public static Date getThisWeekSunday(Date yesterday) {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
        cal.setTime(yesterday);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);//周日
        return cal.getTime();
    }


    /**
     * 获取本周周一的日期
     **/
    public static Date getThisWeekMonday() {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }


    /**
     * 获取本周周一的日期
     **/
    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    /**
     * 获取前N个自然周
     **/
    public static Date getLastNumWeek(Date yesterday, Integer num) {
        int days = -(num - 1) * 7;
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(yesterday));
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }


    public static Map<String, String> dateList(Date date1, Date date2) {
        logger.info("开始日期：{}", convert2String(date1, DATE_FORMAT_DAY));
        logger.info("结束日期：{}", convert2String(date2, DATE_FORMAT_DAY));
        Map<String, String> map = new LinkedHashMap<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        String start = "";
        String end = "";
        while (convert2String(cal.getTime(), DATE_FORMAT_DAY).compareTo(convert2String(date2, DATE_FORMAT_DAY)) <= 0) {
            if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
                start = convert2String(cal.getTime(), DATE_FORMAT_DAY);
            }
            if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || cal.getTime().getTime() == date2.getTime()) {
                end = convert2String(cal.getTime(), DATE_FORMAT_DAY);
                map.put(start.substring(5, 7) + "." + start.substring(8, 10) + "-" + end.substring(5, 7) + "." + end.substring(8, 10), start + "," + end);
            }
            cal.add(Calendar.DATE, 1);
        }
        return map;
    }

    /**
     * 获取过去7天内的日期数组
     *
     * @return 日期数组
     */
    public static List<String> pastDay(String time) {
        List<String> pastDaysList = new ArrayList<>();
        try {
            //我这里传来的时间是个string类型的，所以要先转为date类型的。
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(time);
            for (int i = 6; i >= 0; i--) {
                pastDaysList.add(getPastDate(i, date));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return pastDaysList;
    }


    /**
     * 获取过去7天内的日期数组
     *
     * @return 日期数组
     */
    public static List<String> past7Day(Date date) {
        List<String> pastDaysList = new ArrayList<>();
        for (int i = 6; i >= 0; i--) {
            pastDaysList.add(getPastDate(i, date));
        }
        return pastDaysList;
    }

    /**
     * 获取 获取某年某月 所有日期（yyyy-mm-dd格式字符串）
     **/
    public static List<String> getMonthFullDay(String month) {
        SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
        List<String> fullDayList = new ArrayList<>(32);
        // 获得当前日期对象
        Calendar cal = Calendar.getInstance();
        cal.clear();// 清除信息
        cal.setTime(DateUtil.convert2Date(month, DATE_FORMAT_MONTH));
        int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int j = 1; j <= count; j++) {
            fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime()));
            cal.add(Calendar.DAY_OF_MONTH, 1);
        }
        return fullDayList;
    }


    /**
     * 两个日期之间相隔的日期
     **/
    public static List<String> daysInSelectTime(String startDay, String endDay) {
        List<String> days = new ArrayList<>();
        try {
            //我这里传来的时间是个string类型的，所以要先转为date类型的。
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = sdf.parse(startDay);
            Date date2 = sdf.parse(endDay);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            while (calendar.getTimeInMillis() <= date2.getTime()) {
                days.add(convert2String(calendar.getTime(), "yyyy-MM-dd"));
                calendar.add(Calendar.DATE, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    public static LocalDateTime parse2LocalDateTime(String date, String format) {
        return LocalDateTime.parse(date, DateTimeFormatter.ofPattern(format));
    }

    /**
     * 字符串转LocalDateTime
     *
     * @param date 日期字符串
     * @return 转换后的LocalDateTime
     */
    public static LocalDateTime parse2LocalDateTime(String date) {
        try {
            date = date.replace("年", "-").replace("月", "-").replace("日", "-")
                    .replace("时", ":").replace("分", ":").replace("秒", ":");
            if (date.matches("^(\\d{4})(-\\d{2}){2}$")) {
                return LocalDateTime.of(LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd")), LocalTime.MIN);
            } else if (date.matches("^(\\d{4})(/\\d{2}){2}$")) {
                return LocalDateTime.of(LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd")), LocalTime.MIN);
            } else if (date.matches("^\\d{8}$")) {
                return LocalDateTime.of(LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyyMMdd")), LocalTime.MIN);
            } else if (date.matches("^(\\d{2}/){2}(\\d{4})$")) {
                return LocalDateTime.of(LocalDate.parse(date, DateTimeFormatter.ofPattern("MM/dd/yyyy")), LocalTime.MIN);
            } else if (date.matches("^(\\d{4})(-\\d{2}){2}(\\s)(\\d{2})(:\\d{2}){2}$")) {
                return LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            } else if (date.matches("^(\\d{4})(/\\d{2}){2}(\\s)(\\d{2})(:\\d{2}){2}$")) {
                return LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
            } else if (date.matches("^(\\d{2}/){2}(\\d{4})(\\s)(\\d{2})(:\\d{2}){2}$")) {
                return LocalDateTime.parse(date, DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss"));
            } else if (date.matches("^\\d{14}$")) {
                return LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
            }
        } catch (Exception e) {
            logger.error("未知的日期格式: " + date);
        }
        return LocalDateTime.now();
    }

    /**
     * 下一天
     **/
    public static String getNextDay(String date) {
        try {
            //我这里传来的时间是个string类型的，所以要先转为date类型的。
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Date date1 = sdf.parse(date.replace("-", ""));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            calendar.add(Calendar.DATE, 1);
            return convert2String(calendar.getTime(), "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 下一天
     **/
    public static String getNextDay(String date, String format) {
        try {
            //我这里传来的时间是个string类型的，所以要先转为date类型的。
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date1 = sdf.parse(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            calendar.add(Calendar.DATE, 1);
            return convert2String(calendar.getTime(), format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 下一天
     **/
    public static String getPreDay(String date, String format, int num) {
        try {
            //我这里传来的时间是个string类型的，所以要先转为date类型的。
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date1 = sdf.parse(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            calendar.add(Calendar.DATE, -num);
            return convert2String(calendar.getTime(), format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 20220202--2020-01-01
     **/
    public static String dateFormat(String date, String format1, String format2) {
        if (StringUtils.isBlank(date)) {
            return "";
        }
        String dateFormat = DateUtil.fmtDateToStr(DateUtil.convert2Date(date, format1), format2);
        return dateFormat;

    }

    /**
     * 判断是不是当天的报工、
     * 例如 凌晨四点的报工就是昨天的
     **/
    public static String getTheDay(String time, String format) {
        Date date = DateUtil.convert2Date(time, format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int curHour24 = calendar.get(calendar.HOUR_OF_DAY);
        if (curHour24 < 8) {
            return DateUtil.getPastDate(1, date);
        }
        return convert2String(date, DATE_FORMAT_DAY);
    }

    /**
     * 转成中文年月日
     */
    public static String transfer2ChineseYYYYMMDD(String yyyyMMDD) {
        yyyyMMDD = yyyyMMDD.replaceFirst("\\-", "年");
        yyyyMMDD = yyyyMMDD.replaceFirst("\\-", "月");
        yyyyMMDD += "日";
        return yyyyMMDD;
    }


    public static LocalDate LocalDatePlusDays(String date, String format, Integer days) {
        return LocalDate.parse(date, DateTimeFormatter.ofPattern(DateUtil.DATE_FORMAT_DAY)).plusDays(-1);
    }


}